define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionreportday/index' + location.search,
                    add_url: 'feditionreportday/add',
                    edit_url: 'feditionreportday/edit',
                    del_url: 'feditionreportday/del',
                    multi_url: 'feditionreportday/multi',
                    import_url: 'feditionreportday/import',
                    table: 'fedition_reportday',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'timedate',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'timedate', title: __('Timedate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD"},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'advertising', title: __('Advertising'), operate:'BETWEEN'},
                        {field: 'sales', title: __('Sales'), operate:'BETWEEN'},
                        {field: 'roi', title: __('ROI'), operate:'LIKE',formatter: Table.api.formatter.label},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('正常'),"hidden":__('待核对')}, formatter: Table.api.formatter.status},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('更新金额'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('更新金额'),
                                    title: __('更新金额'),
                                    classname: 'btn btn-sm btn-info btn-magic btn-ajax',
                                    icon: 'fa fa-usd',
                                    confirm: '确认更新吗？',
                                    url: 'feditionreportday/addreportday/?timedate={timedate}',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('操作记录'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('操作记录'),
                                    title: __('操作记录'),
                                    classname: 'btn btn-sm btn-success btn-dialog',
                                    icon: 'fa fa-magic',
                                    url: 'feditionlog/index/?type=6'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log('zamount',data.Statistics.zamount);
                $("#zamount").text(data.Statistics.zamount);
                $("#zadvertising").text(data.Statistics.zadvertising);
                $("#zsales").text(data.Statistics.zsales);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});